package com.demo.cms.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.demo.cms.dao.CategoryDao;
import com.demo.cms.model.Category;
import com.demo.core.dao.impl.base.PageCoreDaoSupport;
import com.demo.core.model.support.GeneralExecuteModel;

public class CategoryDaoImpl extends PageCoreDaoSupport<Category, Long> implements CategoryDao {

	/**
	 * 目录空间 
	 */
	protected final static String CATEGORY_SPACE = "categorySpace.";
	/**
	 * 模型表名
	 */
	protected final static String TABLE_NAME = "T_CMS_CATEGORY";
	/**
	 * 模型表主键ID
	 */
	protected final static String TABLE_PK_NAME = "ID";
	
	@Override
	public int delete(Long id) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		return delete(params);
	}
	
	@Override
	public int delete(Map<String, Object> params) {
		return super.delete(params);
	}
	
	@Override
	public int updateStatus(Category category, Map<String, Object> categoryStatus) {
		categoryStatus.put("category", category);
		return getSqlMapClientTemplate().update(CATEGORY_SPACE + "changeEnabled", categoryStatus);
	}

	@Override
	public int updatePermission(boolean isPermission, Map<String, Object> categoryStatus) {
		Category category = new Category();
		category.setPermission(isPermission);
		categoryStatus.put("category", category);
		return getSqlMapClientTemplate().update(CATEGORY_SPACE + "changePermission", categoryStatus);
	}

	@Override
	public int updateAnalyse(boolean isAnalyse, Map<String, Object> categoryStatus) {
		Category category = new Category();
		category.setAnalyse(isAnalyse);
		categoryStatus.put("category", category);
		return getSqlMapClientTemplate().update(CATEGORY_SPACE + "changeAnalyse", categoryStatus);
	}

	@Override
	public int updateDraft(boolean isDraft, Map<String, Object> categoryStatus) {
		Category category = new Category();
		category.setDraft(isDraft);
		categoryStatus.put("category", category);
		return getSqlMapClientTemplate().update(CATEGORY_SPACE + "changeDraft", categoryStatus);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Category> tree(Map<String, Object> params) {
		return getSqlMapClientTemplate().queryForList(CATEGORY_SPACE + "queryTree", params);
	}

	@Override
	protected String nameSpace() {
		return CATEGORY_SPACE;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Category> findRoot(Map<String, Object> params) {
		return getSqlMapClientTemplate().queryForList(CATEGORY_SPACE + "selectRoot", params);
	}

	@Override
	public Category findRoot(Long channelId) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("channelId", channelId);
		List<Category> categoryList = findRoot(params);
		return categoryList.size() > 0 ? categoryList.get(0) : null;
	}
	
	@Override
	public Category findRoot() {
		Map<String, Object> params = new HashMap<String, Object>(3);
		List<Category> categoryList = findRoot(params);
		return categoryList.size() > 0 ? categoryList.get(0) : null;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Category> queryReverse(Map<String, Object> params) {
		List<Category> categroyList = getSqlMapClientTemplate().queryForList(CATEGORY_SPACE + "queryReverse", params);
		return categroyList;
	}
	
	@Override
	protected void doInit(GeneralExecuteModel<Long> model) {
		model.setPk(TABLE_PK_NAME);
		model.setTable(TABLE_NAME);
	}

}
